package top.lixunda.ecommerce.server.good.api.entity.vo;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.good.api.entity.db.SysGoodCategoryDO;
import top.lixunda.ecommerce.server.good.api.entity.db.SysStoreCategoryDO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 树形结构的店铺目录, 第一个节点为root
 *
 * @author Xumda
 * @version time:2020/1/11 10:55
 */
@ApiModel("树形结构的店铺目录, 第一个节点为root")
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SysStoreCategoryTreeVO {

    /**
     * 当前节点
     */
    private SysStoreCategoryVO node;

    /**
     * 子节点
     */
    private List<SysStoreCategoryTreeVO> children;

    /**
     * 转换
     *
     * @param list 带转换数据
     * @return 转换后的数据
     */
    public static SysStoreCategoryTreeVO fromStoreCategoryDoList(List<SysStoreCategoryDO> list) {
        SysStoreCategoryTreeVO treeVO = new SysStoreCategoryTreeVO();
        if (ObjectValidators.isEmpty(list)) {
            return treeVO;
        }
        // 移除空项
        list = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
        // 获取根节点
        List<SysStoreCategoryDO> rootList = list.stream()
                .filter(item -> SysGoodCategoryDO.ROOT_IS.equals(item.getIsRoot()))
                .sorted(Comparator.comparing(SysStoreCategoryDO::getName))
                .collect(Collectors.toList());
        // 移除根节点
        list.removeAll(rootList);
        treeVO.setChildren(
                rootList.stream()
                        .map(item -> {
                            SysStoreCategoryTreeVO vo = new SysStoreCategoryTreeVO();
                            vo.setNode(SysStoreCategoryVO.fromStoreCategoryDO(item));
                            return vo;
                        })
                        .collect(Collectors.toList()));

        if (ObjectValidators.isEmpty(list) || ObjectValidators.isEmpty(treeVO.getChildren())) {
            return treeVO;
        }

        // 递归
        for (SysStoreCategoryTreeVO vo : treeVO.getChildren()) {
            addChildren(vo, list);
        }

        return treeVO;
    }

    private static void addChildren(SysStoreCategoryTreeVO sysStoreCategoryTreeVO,
                                    List<SysStoreCategoryDO> list) {
        if (ObjectValidators.isEmpty(list)) {
            return;
        }
        List<SysStoreCategoryDO> tempList = new ArrayList<>(list.size());
        for (SysStoreCategoryDO sysStoreCategoryDO : list) {
            if (sysStoreCategoryDO.getParent().equals(sysStoreCategoryTreeVO.getNode().getId())) {
                tempList.add(sysStoreCategoryDO);
            }
        }
        list.removeAll(tempList);
        sysStoreCategoryTreeVO.setChildren(tempList.stream()
                .map(item -> {
                    SysStoreCategoryTreeVO vo = new SysStoreCategoryTreeVO();
                    vo.setNode(SysStoreCategoryVO.fromStoreCategoryDO(item));
                    return vo;
                })
                .collect(Collectors.toList()));

        if (ObjectValidators.isEmpty(sysStoreCategoryTreeVO.getChildren()) || list.size() == 0) {
            return;
        }

        // 递归
        for (SysStoreCategoryTreeVO vo : sysStoreCategoryTreeVO.getChildren()) {
            // 叶节点，跳过
            if (SysGoodCategoryDO.ROOT_IS.equals(vo.getNode().getIsLeaf())) {
                continue;
            }
            addChildren(vo, list);
        }
    }

}
